﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Yyyg.WebUI
{
    public class VerifyCode
    {
        /// <summary>
        /// 验证码内容
        /// </summary>
        public string Code { get; private set; }

        /// <summary>
        /// 验证码图片数据
        /// </summary>
        public byte[] ImgData { get; private set; }


        static char[] character;

        static VerifyCode()
        {
            List<char> cs = new List<char>();
            for (int i = 48; i <= 57; i++) cs.Add((char)i);
            for (int i = 97; i <= 122; i++) cs.Add((char)i);
            for (int i = 65; i <= 90; i++) cs.Add((char)i);
            character = cs.ToArray();
        }

        public VerifyCode()
        {
            Update();
        }

        /// <summary>
        /// 更新验证码
        /// </summary>
        /// <param name="len"></param>
        public void Update(int len = 4)
        {
            string chkCode = string.Empty;
            Random random = new Random();
            //生成随机的验证码
            for (int i = 0; i < len; i++)
            {
                chkCode += character[random.Next(character.Length)];
            }
            this.Code = chkCode;
            this.ImgData = GetVerifyCode();
        }

        /// <summary>
        /// 校验是否正确
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Check(string value)
        {
            return value.Equals(Code);
        }

        /// <summary>
        /// 返回图片验证码
        /// </summary>
        /// <param name="ckCode">验证码内容</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private byte[] GetVerifyCode(int width = 0, int height = 0)
        {
            int codeW = width;
            int codeH = height;
            //设置默认最小值
            if (codeW < 80) codeW = 80;
            if (codeH < 30) codeH = 30;
            //噪点的颜色列表
            Color[] color = { Color.Black, Color.DeepSkyBlue, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
            //字体列表
            string[] font = { "Times New Roman" };
            Random random = new Random(Tools.GetRandomSeed());
            Bitmap bmp = new Bitmap(codeW, codeH);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            //画噪线 
            for (int i = 0; i < 3; i++)
            {
                int x1 = random.Next(codeW);
                int y1 = random.Next(codeH);
                int x2 = random.Next(codeW);
                int y2 = random.Next(codeH);
                Color clr = color[random.Next(color.Length)];
                g.DrawLine(new Pen(clr), x1, y1, x2, y2);
            }
            //画噪点
            for (int i = 0; i < 30; i++)
            {
                int x = random.Next(codeW);
                int y = random.Next(codeH);
                Color clr = color[random.Next(color.Length)];
                g.FillEllipse(new SolidBrush(clr), x, y, 2, 2);
            }
            //画验证码字符串 
            for (int i = 0; i < this.Code.Length; i++)
            {
                string fnt = font[random.Next(font.Length)];
                Font ft = new Font(fnt, 16);
                Color clr = color[random.Next(color.Length)];
                g.DrawString(this.Code[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
            }
            //将验证码图片写入内存流，并将其以 "image/Png" 格式输出 
            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Png);
                return ms.ToArray();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("生成验证码时出现错误:" + ex.Message);
                return null;
            }
            finally
            {
                ms.Dispose();
                g.Dispose();
                bmp.Dispose();
            }
        }

        public override string ToString()
        {
            return this.Code;
        }
    }
}